<html>
<head><meta charset="utf-8"><title>monomorphised go to definition · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html">monomorphised go to definition</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="233304800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233304800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233304800">(Apr 06 2021 at 11:50)</a>:</h4>
<p>Please hold: Got hit by enter to submit</p>



<a name="233305582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233305582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233305582">(Apr 06 2021 at 11:57)</a>:</h4>
<p>I often find myself trying to work out what code gets called in a specific instantiation of a generic function, e.g. in</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Flob</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">              </span><span class="k">fn</span> <span class="nf">flobulate</span><span class="p">(</span><span class="bp">self</span><span class="p">){</span><span class="o">..</span><span class="p">.}</span><span class="w"> </span><span class="c1">//&lt;-E</span>
<span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Flob</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="kt">u32</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">              </span><span class="k">fn</span> <span class="nf">flobulate</span><span class="p">(</span><span class="bp">self</span><span class="p">){</span><span class="o">..</span><span class="p">.}</span><span class="w"> </span><span class="c1">//&lt;- D</span>
<span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">exec</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">Flob</span><span class="o">&gt;</span><span class="p">(</span><span class="n">val</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// &lt;-B</span>
<span class="w">    </span><span class="n">val</span><span class="p">.</span><span class="n">flobulate</span><span class="p">();</span><span class="w"> </span><span class="c1">// &lt;-C</span>
<span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">(){</span><span class="w"></span>
<span class="w">     </span><span class="n">exec</span><span class="p">(</span><span class="mi">10</span><span class="k">u32</span><span class="p">);</span><span class="w"> </span><span class="o">&lt;-</span><span class="n">A</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I would click go to definition on <code>exec</code> at A, which would take me to B. I'd then click go to definition of <code>flobulate</code> at C, which I would want to take me to D, but instead takes me to E, which doesn't actually show me the code which is run. This means that I have to drop what I'm trying to find out, and manually track which types are used in the instantiation I am interest with, trace them though (following associated types manually, for example) and then track down the specific trait implementation which is actually used.<br>
This is not fun, and feels like something rust-analyzer could help with.</p>



<a name="233305845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233305845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233305845">(Apr 06 2021 at 12:00)</a>:</h4>
<p>I mostly want to confirm that this is an issue that other people are having and think solutions could exist for. I suppose really <a href="https://github.com/rust-analyzer/rust-analyzer/issues/4558">rust-analyzer#4558</a> would be a necessary prerequisite.</p>



<a name="233306587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233306587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233306587">(Apr 06 2021 at 12:05)</a>:</h4>
<p>My proposed user experience would be that when you follow a go to definition on a generic function, if specific type information is available (either from this procedure or because the call does not use the current context's generics), the generics the function is used with are pushed onto a stack somewhere. Ideally, going back using mouse 4 or alt-left would pop this stack. These generics would then be used to get the specific type information for the first step as far as possible. These types could also be displayed as inlay hints next to the generics, i.e.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">exec</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">Flob</span><span class="p">{</span><span class="n">inlay</span><span class="w"> </span><span class="n">hint</span>: <span class="o">=</span><span class="kt">u32</span><span class="p">}</span><span class="o">&gt;</span><span class="p">(</span><span class="n">val</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// &lt;-B</span>
<span class="w">    </span><span class="n">val</span><span class="p">.</span><span class="n">flobulate</span><span class="p">();</span><span class="w"> </span><span class="c1">// &lt;-C</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>in my example</p>



<a name="233306763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233306763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233306763">(Apr 06 2021 at 12:07)</a>:</h4>
<p>Oh, I think this is exactly &lt;<a href="https://github.com/rust-analyzer/rust-analyzer/issues/2541#issuecomment-565199436">https://github.com/rust-analyzer/rust-analyzer/issues/2541#issuecomment-565199436</a>&gt;, so it's already on the radar.</p>



<a name="233306927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233306927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233306927">(Apr 06 2021 at 12:08)</a>:</h4>
<p>Yes, I'd love to see this. E-hard + E-fun though.</p>



<a name="233307036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233307036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233307036">(Apr 06 2021 at 12:08)</a>:</h4>
<p><span class="user-mention" data-user-id="200146">@Daniel Mcnab</span> could you post this as an issue? The existing issue is all other the place, we need something more specific here.</p>



<a name="233307109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233307109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233307109">(Apr 06 2021 at 12:09)</a>:</h4>
<p>Should also link to the issue about goto definition going to an impl (one of the most voted for)</p>



<a name="233307323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233307323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233307323">(Apr 06 2021 at 12:11)</a>:</h4>
<p>E-herculean</p>



<a name="233309709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233309709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233309709">(Apr 06 2021 at 12:30)</a>:</h4>
<p><span class="user-mention" data-user-id="133169">@matklad</span> in your example in <a href="https://github.com/rust-analyzer/rust-analyzer/issues/2541#issuecomment-565199436">https://github.com/rust-analyzer/rust-analyzer/issues/2541#issuecomment-565199436</a>, where would you press go to definition the second time? On the <code>{}</code> in the format string?</p>



<a name="233310188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233310188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233310188">(Apr 06 2021 at 12:34)</a>:</h4>
<p>uh, indeed that's a bad example, as it doesn't literarly call <code>fmt</code></p>



<a name="233310249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233310249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233310249">(Apr 06 2021 at 12:34)</a>:</h4>
<p>Added a <a href="https://github.com/rust-analyzer/rust-analyzer/issues/3419#issuecomment-814083408">comment to #3419</a></p>



<a name="233310502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233310502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233310502">(Apr 06 2021 at 12:36)</a>:</h4>
<p>Right, made <a href="https://github.com/rust-analyzer/rust-analyzer/issues/8373">rust-analyzer#8373</a></p>



<a name="233335835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233335835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233335835">(Apr 06 2021 at 15:07)</a>:</h4>
<p>doing this properly will require LSP support, right? at least so the substitution stack can be pushed/popped in the right moments</p>



<a name="233338064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233338064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233338064">(Apr 06 2021 at 15:15)</a>:</h4>
<p>Maybe? If we add some special short-cut here, like "goto def with subst", then we need support. If we somehow piggy back that on top of usual goto def, then we don't need extra support</p>



<a name="233341696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233341696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233341696">(Apr 06 2021 at 15:35)</a>:</h4>
<p>Pushing the stack should be easy enough to track, although we do have to think about the unbounded memory use it implies (since ideally you'd be able to have an infinite sidetrack somewhere else without losing your place, although that might be unreasonable.<br>
Popping would be more difficult, we'd have to intercept <code>workbench.action.navigateBack</code>, which might not be possible in vscode; then make a custom protocol for it.</p>



<a name="233541640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233541640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rpjohnst <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233541640">(Apr 07 2021 at 18:47)</a>:</h4>
<p>another possibility ux-wise comes from vs, which lets you pick the type arguments from a dropdown based on find-all-references: <a href="https://devblogs.microsoft.com/cppblog/visual-studio-c-template-intellisense-populates-based-on-instantiations-in-your-code/">https://devblogs.microsoft.com/cppblog/visual-studio-c-template-intellisense-populates-based-on-instantiations-in-your-code/</a></p>



<a name="233541695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233541695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rpjohnst <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233541695">(Apr 07 2021 at 18:47)</a>:</h4>
<p>no idea if that kind of extra ui is possible with lsp though</p>



<a name="233542068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233542068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233542068">(Apr 07 2021 at 18:50)</a>:</h4>
<p>that looks sick! but also not possible without extra client support</p>



<a name="233549987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233549987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Mcnab <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233549987">(Apr 07 2021 at 19:47)</a>:</h4>
<p>That seems like kind of the reverse - I almost commented that kind of idea, but I hadn't seen the Visual studio implementation of it</p>



<a name="233568454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/monomorphised%20go%20to%20definition/near/233568454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/monomorphised.20go.20to.20definition.html#233568454">(Apr 07 2021 at 21:47)</a>:</h4>
<p>Maybe you could get around it with a code action or custom command but I think this is worth an issue on <a href="https://github.com/microsoft/language-server-protocol/">https://github.com/microsoft/language-server-protocol/</a> for the protocol support for goto def UI changes</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>